/*
 * @Author: 彭林
 * @Date: 2021-05-13 19:55:08
 * @LastEditTime: 2021-07-14 18:23:03
 * @LastEditors: 彭林
 * @Description:
 * @FilePath: /saas-app/app/service/school_user.go
 */
package service

import (
	"saas-app/app/dao"
	"saas-app/app/define"
	"saas-app/app/model"

	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
)

type SchoolUserRepository interface {
	GetSchoolList(user_id uint) g.List
	GetTeacherList(schoolId, campusId, page, limit int) (gdb.Result, int)
	UpdateTeacher(teacherId int, data map[string]interface{}) bool
	CreateTeacher(schoolUser *model.SchoolUser) bool
	GetTeacherInfoByPhone(campus_id uint, phone string) *model.SchoolUser
	CheckTeacher(campus_id uint, phone string) bool
}

type schoolUser struct{}

func SchoolUserService() SchoolUserRepository {
	return &schoolUser{}
}

func (s schoolUser) GetSchoolList(user_id uint) g.List {

	r, err := g.Table("school_user").Fields("school_user.school_id, school.school_name as school_name").Group("school_user.school_id").LeftJoin("school", "school_user.school_id = school.id").Where("user_id = ?", user_id).All()

	if err != nil {
		panic(err)
	}

	if len(r) == 0 {
		panic("学校列表为空")
	}

	return r.List()
}

func (s schoolUser) GetTeacherList(schoolId, campusId, page, limit int) (gdb.Result, int) {

	db := dao.SchoolUser.Where("school_id = ?", schoolId)
	total, err := db.FindCount()

	if err != nil {
		panic(err)
	}

	if campusId != 0 {
		db = db.Where("campus_id = ?", campusId)
	}
	if page > 0 && limit > 0 {
		db = db.Offset((page - 1) * limit).Limit(limit)
	}
	schoolUser, err := db.FindAll()

	if err != nil {
		panic(err)
	}

	return schoolUser, total
}

func (s schoolUser) UpdateTeacher(teacherId int, data map[string]interface{}) bool {
	_, err := dao.SchoolUser.Where("id = ?", teacherId).Update(data)
	return err == nil
}

func (s schoolUser) CreateTeacher(schoolUser *model.SchoolUser) bool {

	if schoolUser.SchoolId <= 0 {
		panic("参数错误")
	}

	if schoolUser.Sex <= 0 {
		panic("参数错误")
	}

	if schoolUser.Phone == "" {
		panic("参数错误")
	}

	if schoolUser.TeacherName == "" {
		panic("参数错误")
	}

	if schoolUser.Identity == "" {
		panic("参数错误")
	}

	// 账号唯一性数据检查
	if ok := SchoolUserService().CheckTeacher(schoolUser.CampusId, schoolUser.Phone); ok {
		panic("老师已经存在")
	}

	// 判断是否已经存在user
	user_id := UserService().CheckPassport(schoolUser.Phone)
	if user_id > 0 {
		schoolUser.UserId = user_id
	} else {
		var SignUpParam define.SignUpParam
		SignUpParam.Nickname = schoolUser.TeacherName
		SignUpParam.Passport = schoolUser.Phone
		SignUpParam.Password = "111111"
		SignUpParam.Password2 = "111111"
		result := UserService().SignUp(&SignUpParam)

		if result == nil {
			panic("注册失败")
		}

		UserId, err := result.LastInsertId()

		if err != nil {
			panic(err)
		}

		schoolUser.UserId = uint(UserId)
	}

	_, err := dao.SchoolUser.Save(schoolUser)

	if err != nil {
		panic(err)
	}

	return true
}

func (s schoolUser) GetTeacherInfoByPhone(campus_id uint, phone string) *model.SchoolUser {

	record, err := dao.SchoolUser.Where("phone = ?", phone).Where("campus_id = ?", campus_id).FindOne()

	if err != nil {
		panic(err)
	}

	user := model.SchoolUser{}
	record.Struct(&user)

	return &user
}

// 检查老师账号是否存在
func (s schoolUser) CheckTeacher(campus_id uint, phone string) bool {

	teacher := SchoolUserService().GetTeacherInfoByPhone(campus_id, phone)

	if teacher != nil && teacher.Id != 0 {
		return true
	}
	return false
}
